import { Component, Input, OnInit } from '@angular/core';
import {Hero} from './hero';
// Keep the Input import for now, you'll remove it later:
import { ActivatedRoute, ParamMap } from '@angular/router';
import { Location }                 from '@angular/common';
import { HeroService } from './hero.service';
import 'rxjs/add/operator/switchMap';

@Component({
  selector: 'hero-detail',
  templateUrl: './hero-detail.component.html'

})
export class HeroDetailComponent implements OnInit{

  constructor(
    private heroService: HeroService,
    private route: ActivatedRoute,
    private location: Location
  ) {}
  // 这里的 hero指什么？
  // 是这里绑定的hero属性<hero-detail [hero]="selectedHero"></hero-detail>
  goBack(): void {
    this.location.back();
  }

  ngOnInit(): void {
    this.route.paramMap
      .switchMap((params: ParamMap) => this.heroService.getHero(+params.get('id')))
      .subscribe(hero => this.hero = hero);
  }


  @Input() hero: Hero;
}
